package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

/**
 * 员工管理模块
 */
@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    //员工登录
@PostMapping("/login")
public R<Employee> login(@RequestBody Employee employee, HttpSession session){
    String username = employee.getUsername();
    String password = employee.getPassword();
    //1.根据用户查询
    Employee e = employeeService.lambdaQuery().eq(Employee::getUsername, username).one();
    if (e==null){
        //说明账号不存在
        return R.error("账号不存在");
    }
    //2.判断用户密码
    if (!StringUtils.equals(e.getPassword(), DigestUtils.md5DigestAsHex(password.getBytes()))){
//说明密码错误
        return R.error("密码错误");
    }
    //3.判断用户状态
    if (e.getStatus()==0){
        //说明用户被禁用
        return R.error("用户被禁用");
    }
    //4.说明登录成功，一定要将用户存入session中
    session.setAttribute("employee",e.getId());
    return R.success(e);
}
    //员工退出
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
    //清空session中的数据
    session.invalidate();
    //响应退出标记
    return R.success("退出成功");
}
    //添加员工
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpSession session){
    //1.数据不全，自己添加
    employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
//    employee.setCreateTime(LocalDateTime.now());
//    employee.setUpdateTime(LocalDateTime.now());
    //从session中获取员工id
    long employeeId = (Long) session.getAttribute("employee");
//    employee.setCreateUser(employeeId);
//    employee.setUpdateUser(employeeId);
        //2.调用添加方法
    employeeService.save(employee);
    return R.success("添加成功");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
    Page<Employee> pageInfo=new Page<>(page,pageSize);

    //select *from employee where name like ? order by update_time limit ?,?;
    employeeService
            .lambdaQuery()
            .like(StringUtils.isNotBlank(name),Employee::getName,name)
            .orderByDesc(Employee::getUpdateTime)
            .page(pageInfo);

    return R.success(pageInfo);
    }
    //修改员工状态

    @PutMapping
    public R<String> updete(@RequestBody Employee employee,HttpSession session){
//    employee.setUpdateTime(LocalDateTime.now());
    long employeeId = (Long) session.getAttribute("employee");
//    employee.setUpdateUser(employeeId);

    employeeService.updateById(employee);
    return R.success("修改成功");
    }
    //根据id查询

    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}




